package com.springboot.Service.musicIpml;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.JSONToken;
import com.springboot.Service.MusicService;
import com.springboot.domain.music;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;

/**
 * @author 权哥
 * @version 1.0
 * @date 2022/5/18 22:41
 */
@Service
public class getSongIdImpl implements MusicService {

    ArrayList<music> lists_ids=new ArrayList<music>();
    ArrayList<music> lists_url=new ArrayList<music>();
    String music_id_json="";  //歌曲Idjson数据
    String music_url_json="";  //在线歌曲url地址json数据
    String music_picUrl_json="";  //歌曲封面url json数据
    String music_mv_json="";   //歌曲视频url json数据

    public getSongIdImpl() {
    }

    //中文转化URL编码
    public static String toUtf8String(String s) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c >= 0 && c <= 255) {
                sb.append(c);
            } else {
                byte[] b;
                try {
                    b = String.valueOf(c).getBytes("utf-8");
                } catch (Exception ex) {
                    System.out.println(ex);
                    b = new byte[0];
                }
                for (int j = 0; j < b.length; j++) {
                    int k = b[j];
                    if (k < 0)
                        k += 256;
                    sb.append("%" + Integer.toHexString(k).toUpperCase());
                }
            }
        }
        return sb.toString();
    }
    @Override
    public void getAllSongId(String KeyWords) throws IOException {
        //根据输入的关键字查找歌曲的id的json数据
        music_id_json=new getJson().Json_data(new music().getAll_music_id_url()+toUtf8String(KeyWords));

        JSONObject all_json = (JSONObject) JSON.parse(music_id_json);
        JSONObject result = (JSONObject) JSON.parse(all_json.getString("result"));
//        System.out.println(all_json.getString("result"));

        JSONArray songs =  JSON.parseArray(result.getString("songs"));

        System.out.println("songs数据行数:"+songs.size());

        for(int i=0;i<songs.size()-10;i++)
        {
            music music_ID=new music();
            //插入歌曲id
            music_ID.setId(songs.getJSONObject(i).getString("id"));
            //插入视频mvid
            music_ID.setMvid(songs.getJSONObject(i).getString("mvid"));
            //获取歌曲的名字
            music_ID.setTitle(songs.getJSONObject(i).getString("name"));
            //测试输出
//            System.out.println("id======="+songs.getJSONObject(i).getString("id"));
//            System.out.println("======mvid======="+songs.getJSONObject(i).getString("mvid"));

            lists_ids.add(music_ID);
        }
        for(music list:lists_ids)
        {
            //获取歌曲在线url
            music_url_json=new getJson().Json_data(new music().getSong_url()+list.getId());
            //获取视频url
            music_mv_json=new getJson().Json_data(new music().getSong_mv_url()+list.getMvid());
            //获取歌曲封面图片url
            music_picUrl_json=new getJson().Json_data(new music().getSong_image_url()+list.getId());

            //获取在线MP3
            JSONObject music=JSON.parseObject(music_url_json);
            JSONArray url=JSON.parseArray(music.getString("data"));
            System.out.println(url.getJSONObject(0).getString("url"));

            //获取在线MV
            JSONObject mv=JSON.parseObject(music_mv_json);
            JSONObject mvurl=JSON.parseObject(mv.getString("data"));
            System.out.println(mvurl.getString("url"));

            //获取在线封面和专辑名
            JSONObject json=JSON.parseObject(music_picUrl_json);
            JSONArray pic_json_list=JSON.parseArray(json.getString("songs"));
            JSONObject pic=JSON.parseObject(pic_json_list.getJSONObject(0).getString("al"));
            System.out.println(pic.getString("picUrl"));
            System.out.println(pic.getString("name"));

            music music_URL=new music();
            music_URL.setTitle(list.getTitle());//获取歌曲的名字
            music_URL.setUrl(url.getJSONObject(0).getString("url"));
            music_URL.setMv_url(mvurl.getString("url"));
            music_URL.setPicUrl(pic.getString("picUrl"));
            music_URL.setAlbum(pic.getString("name"));

            lists_url.add(music_URL);
        }

        System.out.println("list的长度"+lists_ids.size());
        System.out.println("listurl的长度"+lists_url.size());

    }

    @Override
    public ArrayList<music> getURL() {

        return lists_url;
    }

    public static void main(String[] args) throws IOException {

        new getSongIdImpl().getAllSongId("许嵩");

    }

}
